﻿var imageIO;
var imageProcess;

$(function () {
    try {
        imageIO = document.getElementById("ImageIOCtrl");
        imageProcess = document.getElementById("ImageProcessCtrl");
        ScanInitEngine();
        //InitViewer();
    }
    catch (err) {
        alert("页面加载错误." + err);
    }
})

window.onunload = function () {
    UnLoadEngine();
}







var scaner;
var pageCount = 0;//总页数
var currPageIndex = 0;//当前页
var scanfiletype;
var scanerReady = false;
var filePath = new Array();//保存扫描的文件路径

function ScanInitEngine() {
    var iRet;
    try {
        scaner = document.getElementById("ScanCtrl");
        iRet = scaner.InitEngine();
        if (iRet != 0) {
            alert("无法使用，请确定已经申请WebScan的Software Key。");
            $("#scandiv").disable();
            return iRet;
        }
        scaner.ShowProgressIndicator = true;
        scanerReady = true;
    }
    catch (err) {
        alert("加载控件失败,请确认已经正确安装和设置ActiveX控件." + err);
    }
    return iRet;
}

function UnLoadEngine() {
    if (scanerReady) {
        scaner.UnloadEngine();
        scanerReady = false;
    }
}

function ReadSettingToScaner() {
    scaner.ReadConfigFromXmlFile();
}

//打开参数列表
function ShowSettingWin() {
    if (scanerReady && scaner.IsAvailable()) {
        ShowModalDialog('/Setting.htm', window, 500, 350);
    }
    else {
        alert("请使用有效的扫描源。");
    }
}



//选择扫描源
function SelectSource() {
    if (scanerReady) {
        scaner.SelectSource();
    }
    else {
        alert("扫描仪连接异常，控件加载失败。");
    }
}

function ShowOptions() {
    scaner.ShowScanParams("");
}

function CertificatesSetting() {
    scaner.CertificatesSetting();
}



//扫描完成后事件方法
//fileName:文件路径
//index:当次扫描的索引数
//sessionpage:本次扫描引擎加载后的总扫描装数
function ScanCtrl_PageAcquired(fileName, index, sessionpage) {
    //alert(scaner.FileName);
    alert(DemoCtrl.GetBase64(scaner.FileName));
    pageCount++;

    filePath.push(fileName);
    scaner.FileName = scaner.CreateMaxFile(scaner.FileFolder, 0);

    currPageIndex = pageCount;
    document.getElementById("txtCurrFile").innerHTML = currPageIndex;
    document.getElementById("txtTotalFile").innerHTML = pageCount;


}


//扫描
function Scan() {
    try {
        scaner.InitEngine();
        scaner.ScanningProcessImage = true;
        alert(scaner.IsAvailable())
        if (scanerReady && scaner.IsAvailable()) {
            //alert(scaner.FileFolder);
            scaner.FileFolder = "D:\\档案图片\\" + $("#divLB table tbody tr.selected td:eq(1) a").text() + "\\";//设定存储路径
            scaner.UnloadEngine();
            scaner.InitEngine();
            //SetScanType();设置 扫描参数
            scaner.Acquire();
        }
        else {
            alert("扫描仪不可用");
        }
    }
    catch (err) {
        alert(err.message);
    }
}

String.prototype.ToStringByZero = function (count) {
    var str = this;
    while (str.length < count) str = "0" + str;
    return str;
}

function formatDate() {
    var d = "";
    var s = "";
    d = new Date();
    s += d.getYear();
    s += ((d.getMonth() + 1) + "").ToStringByZero(2);
    s += (d.getDate() + "").ToStringByZero(2);
    s += (d.getHours() + "").ToStringByZero(2);
    s += (d.getMinutes() + "").ToStringByZero(2);
    s += (d.getSeconds() + "").ToStringByZero(2);
    return (s);
}

function Clear() {
    filePath.length = 0;
    pageCount = 0;
    currPageIndex = 0;
    document.getElementById("txtCurrFile").innerHTML = "";
    document.getElementById("txtTotalFile").innerHTML = "";
    imageIO.DeleteFolder(scaner.BatchFolder);
    scaner.BatchFolder = "";
    scaner.FileFolder = "";
    scaner.FileName = "";
    viewer.Clear();
}







(function ($) {
    $.fn.disable = function () {
        /// 屏蔽所有元素 
        return $(this).find("*").each(function () {
            $(this).attr("disabled", "disabled");
        });
    }
    $.fn.enable = function () {
        /// 使得所有元素都有效 
        return $(this).find("*").each(function () {
            $(this).removeAttr("disabled");
        });
    }
})(jQuery);


/*转换函数*/
function startt() {
    var imgFile = new FileReader();
    imgFile.readAsDataURL(img.files[0]);
    imgFile.onload = function () {
        var imgData = this.result; //base64数据    
        imgShow.setAttribute('src', imgData);
        conte.value = imgData;
        len.innerHTML += imgData.length;
    }
}

function GetBase() {


    var imgSrc = "D:\\档案图片\\SS-2018-1-00888\\1.tif";
    //imgSrc = "E:\\Project\\TFS_DossierH\\DossierWeb\\档案图片\\诉讼档案\\湖北省人民检察院\\SS-2017-1-00001-00001\\20170914103202576_1.jpg";
    var str = DemoCtrl.GetBase64(imgSrc);
    alert(str);
}